//公共方法

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 用子id得到树形数据id及父级id
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function getTreeId (data, id = 0, idLable = 'id', pid = 'parentId', res = []) {
  for (var i = 0; i < data.length; i++) {
    if (data[i][idLable] === id) {
      res.unshift(data[i][idLable])
      if (data[i][pid] !== -1) {
        getTreeId(data, data[i][pid], idLable, pid, res)
      }
      break
    }
  }
  return res
}

//格式化时间
export function formatDate(now, type) {
			var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
			var year = now.getFullYear().toString();
			var month = (now.getMonth() + 1).toString();
			var day = (now.getDate()).toString();
			var tian = now.getDay().toString();
			var hour = (now.getHours()).toString();
			var minute = (now.getMinutes()).toString();
			var second = (now.getSeconds()).toString();
			if (hour.length == 1) {
				hour = "0" + hour;
			}
			if (minute.length == 1) {
				minute = "0" + minute;
			}
			if (second.length == 1) {
				second = "0" + second;
			}
			if (type == 1) {
				var dateTime = hour + ":" + minute;
			} else if (type == 2) {
				var dateTime = show_day[tian] + " " + hour + ":" + minute
			} else if (type == 3) {
				var dateTime = year + "-" + month + "-" + day
			} else if (type == 4) {
				var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
			} else if (type == 5) {
				var dateTime = show_day[tian];
			}
			return dateTime;
}

/**
* 对象深度克隆
*/
export function clone(obj){
	var that = this;
	if(obj === null) return null;
	if(typeof obj !== 'object') return obj;
	if(obj.constructor===Date) return new Date(obj);
	if(obj.constructor === RegExp) return new RegExp(obj);
	var newObj = new obj.constructor ();  //保持继承链
	for (var key in obj) {
		if (obj.hasOwnProperty(key)) {   //不遍历其原型链上的属性
			var val = obj[key];
			newObj[key] = typeof val === 'object' ? clone(val) : val; // 使用arguments.callee解除与函数名的耦合
		}
	}
	return newObj;
}

/**
 * 获取光标位置
 * @param {DOMElement} element 输入框的dom节点
 * @return {Number} 光标位置
 */
export const getCursorPosition = (element) => {
  let caretOffset = 0
  const doc = element.ownerDocument || element.document
  const win = doc.defaultView || doc.parentWindow
  const sel = win.getSelection()
  if (sel.rangeCount > 0) {
    const range = win.getSelection().getRangeAt(0)
    const preCaretRange = range.cloneRange()
    preCaretRange.selectNodeContents(element)
    preCaretRange.setEnd(range.endContainer, range.endOffset)
    caretOffset = preCaretRange.toString().length
  }
  return caretOffset
}

/**
 * 设置光标位置
 * @param {DOMElement} element 输入框的dom节点
 * @param {Number} cursorPosition 光标位置的值
 */
export const setCursorPosition = (element, cursorPosition) => {
  const range = document.createRange()
  range.setStart(element.firstChild, cursorPosition)
  range.setEnd(element.firstChild, cursorPosition)
  const sel = window.getSelection()
  sel.removeAllRanges()
  sel.addRange(range)
}

/**
* 随机数生成器
*/
export function randomNum(minNum,maxNum){
	switch(arguments.length){
		case 1: 
		  return parseInt(Math.random()*minNum+1,10); 
		  break; 
		case 2: 
		  return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
		  break; 
		default: 
		  return 0; 
		  break;
	}
}